package trss.bencode;

import java.io.Serializable;

public class BString implements BElement, Serializable {

    private static final long serialVersionUID = 4956659844886858771L;
    private byte[] value;

    /**
     * @return the value
     */
    public byte[] getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(byte[] value) {
        this.value = value;
    }

    /**
    * ${@inheritDoc}
    */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    /**
    * ${@inheritDoc}
    */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BString other = (BString) obj;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }

    /**
    * ${@inheritDoc}
    */
    @Override
    public String toString() {
    	String str = "";
    	for (int i = 0; i < value.length && i < 128; i++) {
    		byte c = value[i];
    		if (c < 32 || c == 128) {
    			str += "\\"+c;
    		} else {
    			str += (char) c;
    		}
		}
    	if (str.length() > 128) {
    		str += "...";
    	}
    	return str;
    }
    
}
